import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.border.TitledBorder;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.border.LineBorder;
import java.awt.Color;


public class GameModeView extends JFrame {

	private JPanel contentPane;
	private JRadioButton radioButton5;
	private JRadioButton radioButton6;
	private JRadioButton radioButton7;
	private JRadioButton radioButton8;
	private JButton btnGioca;
	private JRadioButton rdbtnGiganteVsAi;
	private JRadioButton rdbtnNaniVsAi;
	private JRadioButton rdbtnGiganteVsNani;
	private JPanel panel_2;
	private JRadioButton rdbtnFacile;
	private JRadioButton rdbtnMedia;
	private JRadioButton rdbtnDifficile;
	private ButtonGroup group_2;
	
	private class RadioButtonsListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getActionCommand().compareTo("Nani vs AI")==0){
				switch((int)Math.round(Math.random()*(8-5+1)+(5-0.5))){//da' un intero casuale in [5, 8]
				    case 5:
				    	radioButton5.setSelected(true);
				    	break;
				    case 6:
				    	radioButton6.setSelected(true);
				    	break;
				    case 7:
				    	radioButton7.setSelected(true);
				    	break;
				    case 8:
				    	radioButton8.setSelected(true);
				    	break;
				}//switch
				radioButton5.setEnabled(false);
				radioButton6.setEnabled(false);
				radioButton7.setEnabled(false);
				radioButton8.setEnabled(false);
				
				rdbtnFacile.setEnabled(true);
				rdbtnMedia.setEnabled(true);
				rdbtnMedia.setSelected(true);
				rdbtnDifficile.setEnabled(true);
		    }else if(e.getActionCommand().compareTo("Gigante vs AI")==0){
		    	radioButton5.setEnabled(true);
				radioButton6.setEnabled(true);
				radioButton7.setEnabled(true);
				radioButton8.setEnabled(true);

				rdbtnFacile.setEnabled(true);
				rdbtnMedia.setEnabled(true);
				rdbtnMedia.setSelected(true);
				rdbtnDifficile.setEnabled(true);
			}else{
				radioButton5.setEnabled(true);
				radioButton6.setEnabled(true);
				radioButton7.setEnabled(true);
				radioButton8.setEnabled(true);
				
				rdbtnFacile.setEnabled(false);
				rdbtnMedia.setEnabled(false);
				rdbtnDifficile.setEnabled(false);
				group_2.clearSelection();
			}
			
		}

	}

	/**
	 * Create the frame.
	 */
	public GameModeView() {
		setTitle("Caccia al gigante - scelta della modalita' ");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 275);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new GridLayout(4, 1, 0, 5));
		
		JPanel panel_0 = new JPanel();
		panel_0.setBorder(new TitledBorder(null, "modalita'", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(panel_0);
		
		RadioButtonsListener radButtList = new RadioButtonsListener();
		
		rdbtnGiganteVsAi = new JRadioButton("Gigante vs AI");
		rdbtnGiganteVsAi.addActionListener(radButtList);
		panel_0.add(rdbtnGiganteVsAi);
		
		rdbtnNaniVsAi = new JRadioButton("Nani vs AI");
		rdbtnNaniVsAi.addActionListener(radButtList);
		panel_0.add(rdbtnNaniVsAi);
		
		rdbtnGiganteVsNani = new JRadioButton("Gigante vs Nani");
		rdbtnGiganteVsNani.addActionListener(radButtList);
		rdbtnGiganteVsNani.setSelected(true);
		panel_0.add(rdbtnGiganteVsNani);
		
		ButtonGroup group_0 = new ButtonGroup();
		group_0.add(rdbtnGiganteVsAi);
		group_0.add(rdbtnNaniVsAi);
		group_0.add(rdbtnGiganteVsNani);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "posizione iniziale del Gigante", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(panel_1);
		
		radioButton5 = new JRadioButton("5");
		panel_1.add(radioButton5);
		
		radioButton6 = new JRadioButton("6");
		panel_1.add(radioButton6);
		
		radioButton7 = new JRadioButton("7");
		panel_1.add(radioButton7);
		
		radioButton8 = new JRadioButton("8");
		radioButton8.setSelected(true);
		panel_1.add(radioButton8);
		
		ButtonGroup group_1 = new ButtonGroup();
		group_1.add(radioButton5);
		group_1.add(radioButton6);
		group_1.add(radioButton7);
		group_1.add(radioButton8);
		
		panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(new LineBorder(new Color(184, 207, 229)), "difficolta'", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(51, 51, 51)));
		contentPane.add(panel_2);
		
		rdbtnFacile = new JRadioButton("facile");
		panel_2.add(rdbtnFacile);
		
		rdbtnMedia = new JRadioButton("media");
		panel_2.add(rdbtnMedia);
		
		rdbtnDifficile = new JRadioButton("difficile");
		panel_2.add(rdbtnDifficile);
		
		rdbtnFacile.setEnabled(false);
		rdbtnMedia.setEnabled(false);
		rdbtnDifficile.setEnabled(false);
		
		group_2 = new ButtonGroup();
		group_2.add(rdbtnFacile);
		group_2.add(rdbtnMedia);
		group_2.add(rdbtnDifficile);
		
		btnGioca = new JButton("Gioca!");
		contentPane.add(btnGioca);
		
	}

	void addBtnGiocaListener(ActionListener btnGiocaListener) {
		btnGioca.addActionListener(btnGiocaListener);
		
	}

	int getMode() {
		if(rdbtnGiganteVsAi.isSelected()) return 1;
		else if(rdbtnNaniVsAi.isSelected()) return 2;
		else return 3;	
		
	}

	int getGiantInitPos() {
		if(radioButton5.isSelected()) return 5;
		else if(radioButton6.isSelected()) return 6;
		else if(radioButton7.isSelected()) return 7;
		else return 8;
		
	}
	
	String getDifficulty() {
		if(rdbtnFacile.isSelected()) return "facile";
		if(rdbtnMedia.isSelected()) return "media";
		return "difficile";
		
	}

}
